package cn.stylefeng.guns.modular.cb.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.entity.CbFocusCollection;
import cn.stylefeng.guns.modular.cb.mapper.CbFocusCollectionMapper;
import cn.stylefeng.guns.modular.cb.model.params.CbCustomerParam;
import cn.stylefeng.guns.modular.cb.model.params.CbFocusCollectionParam;
import cn.stylefeng.guns.modular.cb.model.params.CbMessageTemplateParam;
import cn.stylefeng.guns.modular.cb.model.params.CbMessagesParam;
import cn.stylefeng.guns.modular.cb.model.result.CbCustomerResult;
import cn.stylefeng.guns.modular.cb.model.result.CbFocusCollectionResult;
import cn.stylefeng.guns.modular.cb.model.result.CbMessageTemplateResult;
import cn.stylefeng.guns.modular.cb.service.CbCustomerService;
import  cn.stylefeng.guns.modular.cb.service.CbFocusCollectionService;
import cn.stylefeng.guns.modular.cb.service.CbMessageTemplateService;
import cn.stylefeng.guns.modular.cb.service.CbMessagesService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ss
 * @since 2021-01-19
 */
@Service
public class CbFocusCollectionServiceImpl extends ServiceImpl<CbFocusCollectionMapper, CbFocusCollection> implements CbFocusCollectionService {

    private static Logger logger = LoggerFactory.getLogger(CbFocusCollectionServiceImpl.class);

    @Autowired
    private CbMessagesService cbMessagesService;
    @Autowired
    private CbMessageTemplateService messageTemplateService;

    @Autowired
    private CbCustomerService cbCustomerService;

    @Override
    public void add(CbFocusCollectionParam param){
        CbFocusCollection entity = getEntity(param);
        this.save(entity);
        param.setId(entity.getId());
    }

    @Override
    public void delete(CbFocusCollectionParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CbFocusCollectionParam param){
        CbFocusCollection oldEntity = getOldEntity(param);
        CbFocusCollection newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CbFocusCollectionResult findBySpec(CbFocusCollectionParam param){
        return null;
    }

    @Override
    public List<CbFocusCollectionResult> findListBySpec(CbFocusCollectionParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CbFocusCollectionParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo getFocusList(CbFocusCollectionParam paramCondition) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.getFocusList(pageContext, paramCondition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo getFansList(CbFocusCollectionParam paramCondition) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.getFansList(pageContext, paramCondition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo getStaffFocusList(CbFocusCollectionParam paramCondition) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.getStaffFocusList(pageContext, paramCondition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo getStaffFansList(CbFocusCollectionParam paramCondition) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.getStaffFansList(pageContext, paramCondition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo getCollectionPositionList(CbFocusCollectionParam paramCondition) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.getCollectionPositionList(pageContext, paramCondition);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 关注收/藏数量
     * @param paramCondition
     * @return
     */
    @Override
    public Integer getFocusCollectionCount(CbFocusCollectionParam paramCondition) {
        return this.baseMapper.getFocusCollectionCount(paramCondition);
    }

    private Serializable getKey(CbFocusCollectionParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CbFocusCollection getOldEntity(CbFocusCollectionParam param) {
        return this.getById(getKey(param));
    }

    private CbFocusCollection getEntity(CbFocusCollectionParam param) {
        CbFocusCollection entity = new CbFocusCollection();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    @Override
    public List<CbFocusCollectionResult> getFocusListByid(CbFocusCollectionParam param){
        return this.baseMapper.getFocusListByid(param);
    }


    /***********************重构***********************/

    @Override
    public void addFocusCollection(CbFocusCollectionParam cbFocusCollectionParam){
        //添加
        try{
            this.add(cbFocusCollectionParam);

            //关注后添加一条消息数据
            CbMessagesParam cbMessagesParam = new CbMessagesParam();
            cbMessagesParam.setMessageUserid(cbFocusCollectionParam.getObjectId());
            cbMessagesParam.setSendUserid(cbFocusCollectionParam.getUserId());//消息发送人
            if (cbFocusCollectionParam.getType()==1){
                //获取模板消息
                CbMessageTemplateParam mtParam = new CbMessageTemplateParam();
                mtParam.setType("MT_GZ");//关注
                JSONObject msgObj = new JSONObject();
                msgObj.put("msg", "您收到一条新的关注消息");
//            String msg = "您收到一条新的关注消息";
                logger.info("CbMessageTemplateParam: {}", mtParam);
                CbMessageTemplateResult mtResult = messageTemplateService.findBySpec(mtParam);
                if (mtResult!=null && !StringUtils.isEmpty(mtResult.getDescription())){
                    String newMsg = mtResult.getDescription();
                    CbCustomerParam param = new CbCustomerParam();
                    param.setId(cbMessagesParam.getSendUserid());
                    CbCustomerResult result = cbCustomerService.getCustomer(param);//消息投递人
//                msg = newMsg.replace("name", result.getName());
                    msgObj.put("msg", newMsg.replace("name", result.getName()));
                }
                cbMessagesParam.setType(1L);
                cbMessagesParam.setMessage(msgObj.toJSONString());
                cbMessagesParam.setStatus(0L);
                cbMessagesParam.setEventid(cbFocusCollectionParam.getId());
                cbMessagesService.add(cbMessagesParam);
            }
        }catch (Exception e){
            logger.error("addFocusCollection error: ", e);
        }
    }

    @Override
    public Integer deleteByUserIdAndObjectId(CbFocusCollectionParam paramCondition) {
        return this.baseMapper.deleteByUserIdAndObjectId(paramCondition);
    }
}
